package com.wtf.dsp.admin.controller;

import com.wtf.dsp.admin.entity.WtfDspSql;
import com.wtf.dsp.admin.model.dto.*;
import com.wtf.dsp.admin.model.vo.SqlVO;
import com.wtf.dsp.admin.service.SqlService;
import com.wtf.dsp.core.api.R;
import com.wtf.dsp.core.model.ExecSqlRequest;
import com.wtf.dsp.core.util.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: DsMapperController
 * Description:
 * date: 2023/7/11 9:57
 *
 * @author linhuanyou
 * @version 1.0
 */
@Api(tags = "SQL语句")
@RestController
@RequestMapping("/sql")
public class SqlController {

    @Autowired
    private SqlService sqlService;

    @ApiOperation(value = "获取日志(分页)")
    @GetMapping("/log/page")
    public R<PageUtils> logPage(@Validated SqlLogPageDTO slPageDTO){
        PageUtils pageUtils = sqlService.logPage(slPageDTO);
        return R.ok(pageUtils);
    }

    @ApiOperation(value = "获取SQL(分页)")
    @GetMapping("/page")
    public R<PageUtils> page(@Validated SqlPageDTO sqlPageDTO){
        PageUtils pageUtils = sqlService.page(sqlPageDTO);
        return R.ok(pageUtils);
    }

    @ApiOperation(value = "获取SQL信息")
    @GetMapping("/info")
    public R<SqlVO> info(@Validated SqlInfoDTO sqlInfoDTO){
        SqlVO sqlVO = sqlService.info(sqlInfoDTO);
        return R.ok(sqlVO);
    }

    @ApiOperation(value = "获取SQL")
    @GetMapping("/list")
    public R list(@Validated SqlListDTO sqlListDTO){
        List<WtfDspSql> dmVOS = sqlService.list(sqlListDTO);
        return R.ok(dmVOS);
    }

    @ApiOperation(value = "新增SQL")
    @PostMapping("/add")
    public R add(@RequestBody @Validated SqlAddDTO sqlAddDTO){
        int add = sqlService.add(sqlAddDTO);
        return R.ok(add);
    }

    @ApiOperation(value = "删除SQL")
    @PostMapping("/del")
    public R remove(@RequestBody @Validated SqlDelDTO sqlDelDTO){
        int del = sqlService.remove(sqlDelDTO);
        return R.ok(del);
    }

    @ApiOperation(value = "更新SQL")
    @PostMapping("/update")
    public R update(@RequestBody @Validated SqlUpdateDTO sqlUpdateDTO){
        int update = sqlService.update(sqlUpdateDTO);
        return R.ok();
    }

    //TODO SQL预执行接口
    @ApiOperation(value = "测试SQL")
    @PostMapping("/test")
    public R test(@RequestBody @Validated SqlTestDTO sqlTestDTO) throws Exception {
        Object result = sqlService.test(sqlTestDTO);
        return R.ok(result);
    }
}
